package controller.fshare;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import controller.fshare.Interface.ClientFileShareControl;
import controller.infocenter.UserInfos;

import model.fshare.Interface.FileDownload;
import model.fshare.Interface.FileTransmistObserver;
import model.fshare.Interface.FileUpload;
import model.fshare.client.FileDownloader;
import model.fshare.client.FileUploader;
import model.fshare.common.CMDDelete;
import model.fshare.common.CMDGetFileList;
import model.fshare.common.Command;
import model.infocenter.ClientUserInformations;
import model.infocenter.ServerInformations;

public class ClientFileShareController implements ClientFileShareControl
{
	FileDownload fDownloader;
	FileUpload fUploader;
	
	public ClientFileShareController()
	{
		fDownloader = new FileDownloader();
		fUploader = new FileUploader();
		
		System.out.println( "======== Client File Share Controller Constructed! =======" );
	}

	
	@Override
	public int upload(File file, FileTransmistObserver observer) 
	{
		UserInfos userInfos = ClientUserInformations.getInstance();
		int roomId = userInfos.getCurRoomId();
		int userId = userInfos.getUserId();
		
		return fUploader.upload( roomId, userId, file, observer );
	}

	
	@Override
	public boolean download(String fileName, FileTransmistObserver observer) 
	{
		UserInfos userInfos = ClientUserInformations.getInstance();
		int roomId = userInfos.getCurRoomId();
		int userId = userInfos.getUserId();
		
		return fDownloader.download( roomId, userId, fileName, observer );
	}

	
	@Override
	public boolean delete(String fileName) 
	{
		UserInfos userInfos = ClientUserInformations.getInstance();
		int roomId = userInfos.getCurRoomId();
		int userId = userInfos.getUserId();
		
		Command deleteCMD = new CMDDelete( roomId, fileName, userId );
		boolean hasDeleted = (Boolean) callForService( deleteCMD );
		return hasDeleted;
	}
	

	@Override
	public ArrayList<String> getAllSharedFilesName() 
	{
		UserInfos userInfos = ClientUserInformations.getInstance();
		int roomId = userInfos.getCurRoomId();
		
		Command getFileListCMD = new CMDGetFileList( roomId );
		@SuppressWarnings("unchecked")
		ArrayList< String > filesNameList = (ArrayList< String >) callForService( getFileListCMD );
		return filesNameList;
	}
	
	
	private Object callForService(Command command)
	{
		Object result = null;
		
		try
		{
			String serverIp = ServerInformations.getInstance().getIP();
			Socket cmdSocket = new Socket( serverIp, 5996 );
			
			ObjectOutputStream oSender = new ObjectOutputStream( cmdSocket.getOutputStream() );
			oSender.writeObject( command );
			oSender.flush();
			
			ObjectInputStream oReader = new ObjectInputStream( cmdSocket.getInputStream() );
			result = oReader.readObject();
			
			cmdSocket.close();
		}
		catch ( UnknownHostException e ) 
		{
			e.printStackTrace();
		} 
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
		
		return result;
	}

}
